#tag Module
Protected Module Globals
	#tag Method, Flags = &h0
		Function ConnectDb() As Boolean
		  mydb=New MySQLCommunityServer
		  
		  mydb.host=app.mPrefs.getstring("dbhost")
		  mydb.port=val(app.mPrefs.getstring("dbport"))
		  mydb.DatabaseName=app.mprefs.getstring("dbschema")
		  mydb.UserName=app.mprefs.getstring("dbuser")
		  mydb.Password=app.mprefs.getstring("dbpwd")
		  
		  if mydb.Connect then
		    
		  else
		    MsgBox mydb.ErrorMessage
		  end
		  
		  return true
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function ExportJPG(extends i as ImageWell) As boolean
		  dim s as string
		  dim j as integer
		  dim rnd as Random
		  
		  rnd=New Random
		  
		  j=rnd.InRange(1,999999)
		  
		  s=format(j,"00000000")
		  
		  if i.Name="Imagewell1" then
		    
		    IMG1File=TempDataFolder.Child(s+".JPG")
		    IMG1File.SaveAsJPEG(i.image)
		    
		  else
		    
		    IMG2File=TempDataFolder.Child(s+".JPG")
		    IMG2File.SaveAsJPEG(i.image)
		    
		  end
		  
		  return true
		  
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function FixedLen(s as string, length as integer) As string
		  
		  while len(s)<length
		    
		    s="0"+s
		    
		  wend
		  
		  return s
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetNextFolder() As string
		  dim rnd as new random
		  dim fi as integer
		  dim ft as string
		  
		  fi=rnd.InRange(0,19)
		  return(format(fi,"00"))
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetNextId(create as boolean) As integer
		  dim rs as recordset
		  dim c as integer
		  
		  rs=mydb.SQLSelect("SELECT OBJECTID FROM "+app.mprefs.getstring("dbschema")+".musicinfo ORDER BY OBJECTID DESC LIMIT 1")
		  
		  if rs<>nil then
		    
		    c=rs.idxfield(1).IntegerValue+1
		    
		    if create then
		      
		      //we create new record here in order to allow almost simultaneous inserts without having to worry that previous insert wasnt completed until next one is entered
		      
		      dim dr as new databaserecord
		      
		      dr.IntegerColumn("OBJECTID")=c
		      
		      mydb.InsertRecord(app.mprefs.getstring("dbschema")+".musicinfo",dr)
		      
		      if mydb.error then MsgBox mydb.errormessage
		      
		      mydb.commit
		      
		      if mydb.error then MsgBox mydb.errormessage
		      
		    end
		    
		    return c
		    
		  end
		  
		  return -1
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function IsPlaying(Extends m as MoviePlayer) As Boolean
		  if m<>nil and m.duration>0 then
		    return true
		  else
		    return false
		  end
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function SecsToTime(sec as double) As string
		  dim h,m,s as integer
		  
		  if sec/3600>1 then
		    h=floor(sec/3600)
		    sec=sec-(h*3600)
		  end
		  
		  if sec/60>1 then
		    m=floor(sec/60)
		    sec=sec-(m*60)
		  end
		  
		  s=sec
		  
		  return format(h,"00")+":"+format(m,"00")+":"+format(s,"00")
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function TimeToSecs(time as string) As integer
		  dim s as integer
		  
		  s=60*60*val(nthfield(time,":",1))
		  s=s+60*val(nthfield(time,":",2))
		  s=s+val(nthfield(time,":",3))
		  
		  return s
		End Function
	#tag EndMethod


	#tag Property, Flags = &h0
		IMG1File As Folderitem
	#tag EndProperty

	#tag Property, Flags = &h0
		IMG2File As Folderitem
	#tag EndProperty

	#tag Property, Flags = &h0
		mydb As MySQLCommunityServer
	#tag EndProperty

	#tag Property, Flags = &h0
		OrigImage1 As Picture
	#tag EndProperty

	#tag Property, Flags = &h0
		OrigImage2 As Picture
	#tag EndProperty

	#tag Property, Flags = &h0
		Session(-1) As FTPSessionClass
	#tag EndProperty

	#tag ComputedProperty, Flags = &h0
		#tag Getter
			Get
			  dim f as folderitem
			  dim s as string
			  
			  s=NthField(app.ExecutableFile.name,".",1)
			  
			  f=SpecialFolder.SharedApplicationData.Child(s)
			  if not f.exists then f.CreateAsFolder
			  
			  return(f)
			End Get
		#tag EndGetter
		TempDataFolder As FolderItem
	#tag EndComputedProperty


	#tag Constant, Name = BSDLicense, Type = String, Dynamic = False, Default = \"Copyright (c) 2009 - 2010\x2C Open Library Player Project. All rights reserved.\r\rRedistribution and use in source and binary forms\x2C with or without modification\x2C are permitted provided that the following conditions are met:\r\r- Redistributions of source code must retain the above copyright notice\x2C this list of conditions and the following disclaimer.\r- Redistributions in binary form must reproduce the above copyright notice\x2C this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\r- Neither the name of the ORGANIZATION nor the names of its \rcontributors may be used to endorse or promote products derived from this software without specific prior written permission.\r\rTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES\x2C INCLUDING\x2C BUT NOT LIMITED TO\x2C THE \rIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT\x2C INDIRECT\x2C INCIDENTAL\x2C SPECIAL\x2C EXEMPLARY\x2C OR CONSEQUENTIAL DAMAGES (INCLUDING\x2C BUT NOT LIMITED TO\x2C PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE\x2C DATA\x2C OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON  ANY THEORY OF LIABILITY\x2C WHETHER IN CONTRACT\x2C STRICT LIABILITY\x2C OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE\x2C EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.", Scope = Public
	#tag EndConstant


	#tag ViewBehavior
		#tag ViewProperty
			Name="Index"
			Visible=true
			Group="ID"
			InitialValue="-2147483648"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Left"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Name"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="OrigImage1"
			Group="Behavior"
			Type="Picture"
		#tag EndViewProperty
		#tag ViewProperty
			Name="OrigImage2"
			Group="Behavior"
			Type="Picture"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Super"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Top"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
	#tag EndViewBehavior
End Module
#tag EndModule
